#!/bin/bash
# run static code checks like pyflakes and pep8
set -eu

echo "1..8"

cd "${srcdir:-.}"
fail=0

#
# pyflakes
#
PYFLAKES="python3 -m pyflakes"

if ! $PYFLAKES /dev/null >/dev/null 2>&1; then
    echo "ok 1 pyflakes pkg tools # SKIP pyflakes3 not installed"

elif $PYFLAKES tools/build-debian-copyright tools/npm-release-time >&2; then
    echo "ok 1 pyflakes pkg tools"

else
    echo "not ok 1 pyflakes pkg tools"
    fail=1
fi

# TODO: there are currently a lot of pyflakes errors like
#   'parent' imported but unused
#   'from testlib import *' used; unable to detect undefined names
# Filter these out until these get fixed properly.
if ! $PYFLAKES /dev/null >/dev/null 2>&1; then
    echo "ok 2 pyflakes test # SKIP pyflakes3 not installed"

else
    out=$($PYFLAKES test/ 2>&1 | grep -Ev "(unable to detect undefined names|defined from star imports|'parent' imported but unused)") || true
    if [ -n "$out" ]; then
        echo "$out" >&2
        echo "not ok 2 pyflakes test"
        fail=1
    else
        echo "ok 2 pyflakes test"
    fi
fi

#
# wrongly marked translatable strings
#
if out=$(find src/ pkg/ -name '*.js' -o -name '*.jsx' | xargs grep -n -E "(gettext|_)\(['\`]"); then
    echo 'ERROR: translatable strings must be marked with _("")' >&2
    echo "$out" >&2
    echo "not ok 3 js-translatable-strings"
    fail=1
elif out=$(grep -r 'translatable=["'\'']yes' pkg/ doc/); then
    echo "ERROR: 'translatable' HTML attribute is invalid, use 'translate' instead"
    echo "$out" >&2
    echo "not ok 3 js-translatable-strings"
    fail=1
else
    echo "ok 3 js-translatable-strings"
fi

#
# Unsafe content-security-policy
#
# It's dangerous to have 'unsafe-inline' or 'unsafe-eval' in our
# content-security-policy entries. This is the browser equivalent
# of setenforce 0

shopt -s nullglob
safe=yes
for d in pkg/*; do
    if ! test -f "$d"/content-security-policy.override; then
        if grep --color=auto -HE 'content-security-policy.*(\*|unsafe)' "$d"/*.json* /dev/null; then
            safe="no"
        fi
    fi
done

if [ $safe == "no" ]; then
   echo "not ok 4 unsafe-security-policy"
   fail=1
else
    echo "ok 4 unsafe-security-policy"
fi

#
# Bad paths or modifications in patternfly for fonts
#
cssfiles="$(echo dist/*/*.css)"
if [ -z "${cssfiles}" ]; then
    echo "ok 5 patternfly-font-paths # skip dist/ not built"
elif grep --color=auto "\.\./fonts/OpenSans\|fonts/.*eot\|truetype" ${cssfiles} >&2; then
    echo "ERROR: invalid patternfly fonts paths found" >&2
    echo "not ok 5 patternfly-font-paths"
    fail=1
else
    echo "ok 5 patternfly-font-paths"
fi

# Valid JSON files
if [ -e .git ]; then
    for f in $(git ls-tree --name-only -r --full-name HEAD | grep '\.json$'); do
        if ! python3 -m json.tool $f /dev/null; then
            echo "ERROR: $f is invalid JSON" >&2
            echo "not ok 6 json-verify"
            fail=1
        fi
    done
    echo "ok 6 json-verify"
else
    echo "ok 6 json-verify # SKIP not a git tree"
fi

# pycodestyle/pep8 python syntax check
if python3 -m pycodestyle /dev/null >/dev/null 2>&1; then
    PYTHON_STYLE_CHECKER="pycodestyle"
elif python3 -m pep8 /dev/null >/dev/null 2>&1; then
    PYTHON_STYLE_CHECKER="pep8"
fi

if [ -z "${PYTHON_STYLE_CHECKER-}" ]; then
    echo "ok 7 pycodestyle test # SKIP pycodestyle not installed"
elif [ ! -e .git ]; then
    echo "ok 7 pycodestyle test # SKIP not a git tree"
else
    # all normal files which are either:
    #   - filename is *.py
    #   - executable matching ^#!/usr/bin/python3
    PYFILES="$(git grep -lI '^#!.*python3') $(git ls-files "*.py")"
    # W504 and W503 are mutually exclusive (and both disabled by default)
    # explicitly giving '--ignore W504' here is actually more strict than the default
    out=$(python3 -m "$PYTHON_STYLE_CHECKER" --max-line-length=415 --ignore W504 $PYFILES) || true
    if [ -n "$out" ]; then
        echo "$out" >&2
        echo "not ok 7 $PYTHON_STYLE_CHECKER test"
        fail=1
    else
        echo "ok 7 $PYTHON_STYLE_CHECKER test"
    fi
fi

if [ ! -e .git ]; then
    echo 'ok 8 node_modules freshness # SKIP not a git tree'
elif V=0 tools/node-modules test_static_code; then
    echo 'ok 8 node_modules freshness'
else
    echo "not ok 8 node_modules freshness"
    fail=1
fi

exit $fail
